package cn.x.kindergarten.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.x.kindergarten.domain.Teacher;
import cn.x.kindergarten.service.TeacherService;
import cn.x.kindergarten.util.Tools;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@Resource(name="teacherService")
	public TeacherService teacherService;
	
	@RequestMapping("/listTeacher.action")
	public String listTeacher(Model model){
		List<Teacher> teacherList = this.teacherService.queryAll();
		System.out.println(teacherList.size());
		model.addAttribute("teacherList", teacherList);
		return "listTeacher";
	}
	
	@RequestMapping("/addUI.action")
	public String addUI(){
		return "addTeacherUI";
	}
	
	@RequestMapping("/addTeacher.action")
	public String addTeacher(Teacher teacher){
		teacher.setId(Tools.genID());
		this.teacherService.insert(teacher);
		return "redirect:/teacher/listTeacher.action";
	}
	
	@RequestMapping("/deleteTeacher.action")
	public String deleteTeacher(String id){
		this.teacherService.delete(id);
		return "redirect:/teacher/listTeacher.action";
	}
	
	@RequestMapping("/updateTeacherUI.action")
	public String updateTeacherUI(String id, Model model){
		Teacher teacher = this.teacherService.query(id);
		model.addAttribute("teacher", teacher);
		return "updateTeacherUI";
	}
	
	@RequestMapping("/updateTeacher.action")
	public String updateTeacher(Teacher teacher){
		this.teacherService.update(teacher);
		return "redirect:/teacher/listTeacher.action";
	}

}
